package com.tedu.demo1;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;

/**
 * Author：毕龙
 * Date: 2024/6/14 9:24
 */
public class TestJdbc4 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        User user = new User(4, "赵六'", "123121", new Date());

        //1.获取驱动Driver
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取链接
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Chongqing";
        String username = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url,username,password );

        //sql语句
        //sql语句
        String sql = "select id, username, password, birthday from user";
        //3.获取Statement语句
        PreparedStatement pstm = connection.prepareStatement(sql);

        //4.执行sql语句
        ResultSet rs = pstm.executeQuery();

        //5.处理查询结果
        //rs.next 判断有没有下一个数据
        //创建一个集合
        ArrayList<User> list = new ArrayList<>();

        while (rs.next()){
            int id = rs.getInt(1);
            String name = rs.getString(2);
            String pass = rs.getString(3);
            java.sql.Date date = rs.getDate(4);
            list.add(new User(id,name ,pass ,date));
        }

        list.forEach(System.out::println);

        //6.资源释放
        rs.close();
        pstm.close();
        connection.close();

    }
}
